/**
 * 用户api服务接口
 * @author lian
 */

import { UsersApi } from '@/common/constants/request-api'
import { Method } from '@/common/enum/request-methods'
import type { Activity } from '@/common/types/activity'
import type { Dynamic } from '@/common/types/dynamic'
import type { Projects } from '@/common/types/projects'
import type { LoginParams, SmsLoginParams } from '@/common/types/request'
import type { OperateItem, User } from '@/common/types/user'

/**
 * 微信授权一键登录
 * @param data 登录参数
 * @author lian
 */
export const loginWeChatService = (data: LoginParams) => {
  return useRequest<string>({
    method: Method.POST,
    url: UsersApi.LOGIN_WE_CHAT,
    data,
  })
}

/**
 * 获取当前登录用户参与的项目
 * @author lian
 */
export const getUserProjectsService = () => {
  return useRequest<Projects[]>({
    method: Method.GET,
    url: UsersApi.GET_MY_PROJECTS,
  })
}

/**
 * 获取当前登录用户参与的志愿者活动
 * @author lian
 */
export const getUserActivityService = () => {
  return useRequest<Activity[]>({
    method: Method.GET,
    url: UsersApi.GET_MY_ACTIVITIES,
  })
}

/**
 * 获取当前登录用户发布的动态
 */
export const getUserDynamicService = () => {
  return useRequest<Dynamic[]>({
    method: Method.GET,
    url: UsersApi.GET_MY_DYNAMICS,
  })
}

/**
 * 获取当前登录用户信息
 */
export const getUserInfoByIdService = () => {
  return useRequest<User>({
    method: Method.GET,
    url: UsersApi.GET_MY_INFO,
  })
}

/**
 * 更新当前登录用户信息
 * @param data 更新的用户信息
 */
export const updateUserInfoByIdService = (data: Optional<User>) => {
  return useRequest({
    method: Method.PUT,
    url: UsersApi.UPDATE_USER_INFO,
    data,
  })
}

/**
 * 一键登录管理员账号
 */
export const loginAdminService = () => {
  return useRequest<string>({
    method: Method.POST,
    url: UsersApi.LOGIN_ADMIN,
  })
}

/**
 * 更新当前用户积分
 * @param points 积分
 */
export const updateUserPointService = (points: number) => {
  const url = replaceParams(UsersApi.UPDATE_USER_POINTS, [
    {
      paramName: 'points',
      value: points,
    },
  ])

  return useRequest<boolean>({
    method: Method.PUT,
    url,
  })
}

/**
 * 根据当前用户角色获取操作列表
 * @returns
 */
export const getUserOperatesByRoleService = () => {
  return useRequest<OperateItem[]>({
    method: Method.GET,
    url: UsersApi.GET_USERS_OPERATE_LIST,
  })
}

/**
 * 发送手机验证码
 * @param phoneNumber 手机号码
 */
export const sendSmsCodeService = (phoneNumber: number) => {
  return useRequest({
    method: Method.POST,
    url: UsersApi.SEND_SMS,
    data: {
      phoneNumber,
    },
  })
}

/**
 * 短信验证码登录
 * @param data 登录参数
 * @returns
 */
export const smsCodeLoginService = (data: SmsLoginParams) => {
  return useRequest<string>({
    method: Method.POST,
    url: UsersApi.SMS_LOGIN,
    data,
  })
}
